/*
 * Main.java
 *
 * Created on July 3, 2007
 *
 * @author Alex Riedler (Ultrahex)
 *
 * @description
 *      Main JFrame for Editor Window
 *           
 */

package ca.compsci.opent.ide;

import javax.swing.text.*;
import javax.swing.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import java.io.*;

public class MainFrame extends javax.swing.JFrame {
    
    openTFile mainFile;
    
    UndoManager undoManager = new UndoManager();
    
    public MainFrame() {
        initComponents();
        LineNumber lineNumber = new LineNumber(textArea);
	jScrollPane1.setRowHeaderView(lineNumber);
        textArea.setDocument(new HighlightedDocument());
        //textArea.getDocument().addUndoableEditListener(new UndoableEditListener());
        pack();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        mainToolbar = new javax.swing.JToolBar();
        runButton = new javax.swing.JButton();
        pauseButton = new javax.swing.JButton();
        openButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        indentButton = new javax.swing.JButton();
        printButton = new javax.swing.JButton();
        cutButton = new javax.swing.JButton();
        copyButton = new javax.swing.JButton();
        pasteButton = new javax.swing.JButton();
        findButton = new javax.swing.JButton();
        findNextButton = new javax.swing.JButton();
        replaceButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextPane(){
            // Make Text Area always same size as scrollpane, unless text inside is bigger
            public void setSize(java.awt.Dimension d) {
                if (d.width < getParent().getSize().width) {
                    d.width = getParent().getSize().width;
                }
                super.setSize(d);
            }

            // Turn "Off" Line Wrapping
            public boolean getScrollableTracksViewportWidth(){
                return false;
            }

            public void gotoLine(int line) {
                Element root = getDocument().getDefaultRootElement();
                line = Math.max(line, 1);
                line = Math.min(line, root.getElementCount());
                setCaretPosition( root.getElement( line - 1 ).getStartOffset() );
            }

        };
        menuBar = new javax.swing.JMenuBar();
        file = new javax.swing.JMenu();
        newFile = new javax.swing.JMenuItem();
        openFile = new javax.swing.JMenuItem();
        recentFiles = new javax.swing.JMenu();
        exampleFile = new javax.swing.JMenuItem();
        closeFile = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JSeparator();
        saveFile = new javax.swing.JMenuItem();
        saveFileAs = new javax.swing.JMenuItem();
        revertSaved = new javax.swing.JMenuItem();
        jSeparator9 = new javax.swing.JSeparator();
        print = new javax.swing.JMenuItem();
        pageSetup = new javax.swing.JMenuItem();
        jSeparator7 = new javax.swing.JSeparator();
        preferences = new javax.swing.JMenuItem();
        jSeparator8 = new javax.swing.JSeparator();
        exit = new javax.swing.JMenuItem();
        edit = new javax.swing.JMenu();
        undo = new javax.swing.JMenuItem();
        redo = new javax.swing.JMenuItem();
        jSeparator10 = new javax.swing.JSeparator();
        cut = new javax.swing.JMenuItem();
        copy = new javax.swing.JMenuItem();
        paste = new javax.swing.JMenuItem();
        jSeparator11 = new javax.swing.JSeparator();
        selectAll = new javax.swing.JMenuItem();
        jSeparator12 = new javax.swing.JSeparator();
        commentRange = new javax.swing.JMenuItem();
        uncommentRange = new javax.swing.JMenuItem();
        jSeparator13 = new javax.swing.JSeparator();
        indent = new javax.swing.JMenuItem();
        search = new javax.swing.JMenu();
        find = new javax.swing.JMenuItem();
        findNext = new javax.swing.JMenuItem();
        Replace = new javax.swing.JMenuItem();
        jSeparator14 = new javax.swing.JSeparator();
        goTo = new javax.swing.JMenuItem();
        run = new javax.swing.JMenu();
        runFile = new javax.swing.JMenuItem();
        runFileArgs = new javax.swing.JMenuItem();
        help = new javax.swing.JMenu();
        reference = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JSeparator();
        about = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OpenT");
        runButton.setText("Run");
        mainToolbar.add(runButton);

        pauseButton.setText("Pause");
        mainToolbar.add(pauseButton);

        mainToolbar.addSeparator();
        openButton.setText("Open");
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        mainToolbar.add(openButton);

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        mainToolbar.add(saveButton);

        mainToolbar.addSeparator();
        indentButton.setText("Indent");
        mainToolbar.add(indentButton);

        printButton.setText("Print");
        mainToolbar.add(printButton);

        mainToolbar.addSeparator();
        cutButton.setText("Cut");
        cutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cutActionPerformed(evt);
            }
        });

        mainToolbar.add(cutButton);

        copyButton.setText("Copy");
        copyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyActionPerformed(evt);
            }
        });

        mainToolbar.add(copyButton);

        pasteButton.setText("Paste");
        pasteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pasteActionPerformed(evt);
            }
        });

        mainToolbar.add(pasteButton);

        mainToolbar.addSeparator();
        findButton.setText("Find");
        mainToolbar.add(findButton);

        findNextButton.setText("Find Next");
        mainToolbar.add(findNextButton);

        replaceButton.setText("Replace");
        mainToolbar.add(replaceButton);

        getContentPane().add(mainToolbar, java.awt.BorderLayout.NORTH);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(500, 400));
        textArea.setFont(new java.awt.Font("Courier New", 0, 12));
        jScrollPane1.setViewportView(textArea);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        file.setText("File");
        newFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newFile.setText("New");
        file.add(newFile);

        openFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openFile.setText("Open...");
        openFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        file.add(openFile);

        recentFiles.setText("Recent Files");
        exampleFile.setText("example.t");
        recentFiles.add(exampleFile);

        file.add(recentFiles);

        closeFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        closeFile.setText("Close");
        file.add(closeFile);

        file.add(jSeparator6);

        saveFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveFile.setText("Save");
        saveFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        file.add(saveFile);

        saveFileAs.setText("Save As...");
        saveFileAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveFileAsActionPerformed(evt);
            }
        });

        file.add(saveFileAs);

        revertSaved.setText("Revert to Saved");
        revertSaved.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                revertSavedActionPerformed(evt);
            }
        });

        file.add(revertSaved);

        file.add(jSeparator9);

        print.setText("Print");
        file.add(print);

        pageSetup.setText("Page Setup");
        file.add(pageSetup);

        file.add(jSeparator7);

        preferences.setText("Preferences");
        file.add(preferences);

        file.add(jSeparator8);

        exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        exit.setText("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        file.add(exit);

        menuBar.add(file);

        edit.setText("Edit");
        undo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
        undo.setText("Undo");
        undo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                undoActionPerformed(evt);
            }
        });

        edit.add(undo);

        redo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
        redo.setText("Redo");
        redo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redoActionPerformed(evt);
            }
        });

        edit.add(redo);

        edit.add(jSeparator10);

        cut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        cut.setText("Cut");
        cut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cutActionPerformed(evt);
            }
        });

        edit.add(cut);

        copy.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        copy.setText("Copy");
        copy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyActionPerformed(evt);
            }
        });

        edit.add(copy);

        paste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        paste.setText("Paste");
        paste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pasteActionPerformed(evt);
            }
        });

        edit.add(paste);

        edit.add(jSeparator11);

        selectAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        selectAll.setText("Select All");
        selectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectAllActionPerformed(evt);
            }
        });

        edit.add(selectAll);

        edit.add(jSeparator12);

        commentRange.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_5, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        commentRange.setText("Comment Range");
        edit.add(commentRange);

        uncommentRange.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_5, java.awt.event.InputEvent.CTRL_MASK));
        uncommentRange.setText("Uncomment Range");
        edit.add(uncommentRange);

        edit.add(jSeparator13);

        indent.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        indent.setText("Indent File");
        edit.add(indent);

        menuBar.add(edit);

        search.setText("Search");
        find.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        find.setText("Find");
        search.add(find);

        findNext.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        findNext.setText("Find Next");
        search.add(findNext);

        Replace.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        Replace.setText("Replace");
        search.add(Replace);

        search.add(jSeparator14);

        goTo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
        goTo.setText("Goto Line...");
        goTo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goToActionPerformed(evt);
            }
        });

        search.add(goTo);

        menuBar.add(search);

        run.setText("Run");
        runFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        runFile.setText("Run");
        run.add(runFile);

        runFileArgs.setText("Run with Args...");
        run.add(runFileArgs);

        menuBar.add(run);

        help.setText("Help");
        reference.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F10, 0));
        reference.setText("openT Reference");
        help.add(reference);

        help.add(jSeparator5);

        about.setText("About openT");
        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });

        help.add(about);

        menuBar.add(help);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void revertSavedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_revertSavedActionPerformed
        if (mainFile != null) {
            textArea.setText(mainFile.toString());
        }
    }//GEN-LAST:event_revertSavedActionPerformed

    private void redoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redoActionPerformed
        undoManager.redo();
    }//GEN-LAST:event_redoActionPerformed

    private void undoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_undoActionPerformed
        undoManager.undo();
    }//GEN-LAST:event_undoActionPerformed

    private void saveFileAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveFileAsActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showSaveDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File f = fileChooser.getSelectedFile();
            if (f.exists()) {
                JOptionPane.showConfirmDialog(null,"Overwrite existing file?","Allow Overwrite",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
            } else {
                mainFile = new openTFile(f);
            }
        }
        
        mainFile.saveFile(textArea.getText());
    }//GEN-LAST:event_saveFileAsActionPerformed

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        if (mainFile == null) {
            JFileChooser fileChooser = new JFileChooser ();
            int result = fileChooser.showSaveDialog(this);
            if (result == JFileChooser.APPROVE_OPTION) {
                File f = fileChooser.getSelectedFile();
                if (f.exists()) {
                    JOptionPane.showConfirmDialog(null,"Overwrite existing file?","Confirm Overwrite",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
                } else {
                    mainFile = new openTFile(f);
                }
            }
        }
            
        mainFile.saveFile(textArea.getText());
    }//GEN-LAST:event_saveActionPerformed

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
            JFileChooser fileChooser = new JFileChooser();
            int result = fileChooser.showOpenDialog(this);

            if (result == JFileChooser.APPROVE_OPTION) {
                File f = fileChooser.getSelectedFile();
                if (f.isFile() && f.canRead()) {
                    mainFile = new openTFile(f);
                    textArea.setText(mainFile.toString());
                } else {
                    JOptionPane.showMessageDialog(this, "Error opening file: " + f, "", JOptionPane.ERROR_MESSAGE);
                }
            }
    }//GEN-LAST:event_openButtonActionPerformed

    private void goToActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goToActionPerformed
        new GotoDialog(textArea).setVisible(true);
    }//GEN-LAST:event_goToActionPerformed

    private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
        new AboutFrame().setVisible(true);
    }//GEN-LAST:event_aboutActionPerformed

    private void selectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllActionPerformed
        textArea.selectAll();
    }//GEN-LAST:event_selectAllActionPerformed

    private void pasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteActionPerformed
        textArea.paste();
    }//GEN-LAST:event_pasteActionPerformed

    private void copyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyActionPerformed
        textArea.copy();
    }//GEN-LAST:event_copyActionPerformed

    private void cutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cutActionPerformed
        textArea.cut();
    }//GEN-LAST:event_cutActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        System.exit(1);
    }//GEN-LAST:event_exitActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Replace;
    private javax.swing.JMenuItem about;
    private javax.swing.JMenuItem closeFile;
    private javax.swing.JMenuItem commentRange;
    private javax.swing.JMenuItem copy;
    private javax.swing.JButton copyButton;
    private javax.swing.JMenuItem cut;
    private javax.swing.JButton cutButton;
    private javax.swing.JMenu edit;
    private javax.swing.JMenuItem exampleFile;
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenu file;
    private javax.swing.JMenuItem find;
    private javax.swing.JButton findButton;
    private javax.swing.JMenuItem findNext;
    private javax.swing.JButton findNextButton;
    private javax.swing.JMenuItem goTo;
    private javax.swing.JMenu help;
    private javax.swing.JMenuItem indent;
    private javax.swing.JButton indentButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator10;
    private javax.swing.JSeparator jSeparator11;
    private javax.swing.JSeparator jSeparator12;
    private javax.swing.JSeparator jSeparator13;
    private javax.swing.JSeparator jSeparator14;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JSeparator jSeparator7;
    private javax.swing.JSeparator jSeparator8;
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JToolBar mainToolbar;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newFile;
    private javax.swing.JButton openButton;
    private javax.swing.JMenuItem openFile;
    private javax.swing.JMenuItem pageSetup;
    private javax.swing.JMenuItem paste;
    private javax.swing.JButton pasteButton;
    private javax.swing.JButton pauseButton;
    private javax.swing.JMenuItem preferences;
    private javax.swing.JMenuItem print;
    private javax.swing.JButton printButton;
    private javax.swing.JMenu recentFiles;
    private javax.swing.JMenuItem redo;
    private javax.swing.JMenuItem reference;
    private javax.swing.JButton replaceButton;
    private javax.swing.JMenuItem revertSaved;
    private javax.swing.JMenu run;
    private javax.swing.JButton runButton;
    private javax.swing.JMenuItem runFile;
    private javax.swing.JMenuItem runFileArgs;
    private javax.swing.JButton saveButton;
    private javax.swing.JMenuItem saveFile;
    private javax.swing.JMenuItem saveFileAs;
    private javax.swing.JMenu search;
    private javax.swing.JMenuItem selectAll;
    private javax.swing.JTextPane textArea;
    private javax.swing.JMenuItem uncommentRange;
    private javax.swing.JMenuItem undo;
    // End of variables declaration//GEN-END:variables
    
}
